<?php

namespace App\Http\Controllers\Home;

use App\Models\HelpCategorys;
use App\Models\Helps;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HelpController extends Controller
{
    /**
     * 帮助中心列表页面
     * @param home.help.helpList
     */
    public function helpList()
    {
        $title = '帮助中心列表';
        return view('home.help.list',compact('title'));
    }
    /**
     * 帮助中心详情页面
     * @param home.help.helpDetails
     */
    public function helpDetails()
    {
        $title = '帮助中心详情';
        return view('home.help.details',compact('title'));
    }

    /**
     * 帮助中心分类
     * @param home.help.helpCateApi
     */
    public function helpCateApi(Request $request)
    {
        try {
            $lists = HelpCategorys::getCascaderList();
            return $this->writeJson(0, $lists);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 帮助中心列表
     * @param home.help.helpListApi
     * @param page 页码
     * @param cate_pid 分类id
     */
    public function helpListApi(Request $request)
    {
        try {
            $params = [
                'cate_pid' => (int)$request->get('cate_pid'),
            ];
            $page = (int)$request->get('page');
            $lists = Helps::allLists($params,['*'],$page,10);
            foreach ($lists as $key=>$val){
                $lists[$key]['start_at'] = trim(@date('Y-m-d', strtotime($val['created_at'])));
            }

            return $this->writeJson(0, $lists);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * 帮助中心详情
     * @param home.help.helpDetailsApi
     * @param id 内容id
     * @return  title 标题
     * @return  content 内容
     * @return  created_at 发布时间
     */
    public function helpDetailsApi(Request $request)
    {
        try {
            $params = [
                'id' => (int)$request->get('id',1),
            ];
            $lists = Helps::details($params);

            return $this->writeJson(0, $lists);
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

}
